package cn.sxt.bus.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.sxt.bus.domain.Customer;
import cn.sxt.bus.service.CustomerService;
import cn.sxt.bus.vo.CustomerVo;
import cn.sxt.sys.utils.DataGridView;

@Controller
@RequestMapping("customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;

	// 跳到系统公告管理customerManager.jsp
	@RequestMapping("toCustomerManager")
	public String toCustomerManager() {
		return "business/customer/customerManager";
	}

	// 登陆系统公告全查询，分页，模糊
	@RequestMapping("queryAllCustomer")
	@ResponseBody
	public DataGridView queryAllCustomer(CustomerVo customerVo) {
		return customerService.queryAllCustomer(customerVo);
	}

	// 跳添加页面
	@RequestMapping("toAddCustomer")
	public String toAddCustomer() {
		return "business/customer/customerAdd";
	}

	// 添加
	@RequestMapping("addCustomer")
	@ResponseBody
	public Map<String, Object> addCustomer(CustomerVo customerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = customerService.addCustomer(customerVo);
			if (i > 0) {
				msg = "添加成功";
			} else {
				msg = "添加失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "添加失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 跳修改页面，查回显信息
	@RequestMapping("toUpdateCustomer")
	public String toUpdateCustomer(CustomerVo customerVo, Model model) {
		Customer customer = customerService.queryCustomerById(customerVo.getId());
		model.addAttribute("customer", customer);
		return "business/customer/customerUpdate";
	}
	
	// 修改
	@RequestMapping("updateCustomer")
	@ResponseBody
	public Map<String, Object> updateCustomer(CustomerVo customerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = customerService.updateCustomer(customerVo);
			if (i > 0) {
				msg = "修改成功";
			} else {
				msg = "修改失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "修改失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 删除系统公告
	@RequestMapping("deleteCustomer")
	@ResponseBody
	public Map<String, Object> deleteCustomer(CustomerVo customerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			int i = customerService.deleteCustomerById(customerVo);
			if (i > 0) {
				msg = "删除成功";
			} else {
				msg = "删除失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}

	// 批量删除系统公告
	@RequestMapping("batchDeleteCustomer")
	@ResponseBody
	public Map<String, Object> batchDeleteCustomer(CustomerVo customerVo) {
		Map<String, Object> map = new HashMap<>();
		String msg = "";
		try {
			Integer[] ids = customerVo.getIds();
			if (ids != null && ids.length > 0) {
				for (int i = 0; i < ids.length; i++) {
					customerVo.setId(ids[i]);
					int j = customerService.deleteCustomerById(customerVo);
					if (j > 0) {
						msg = "删除成功";
					} else {
						msg = "删除失败";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "删除失败";
		}
		map.put("msg", msg);
		return map;
	}
}
